package jp.gr.java_conf.ryz.summarize.client.presenter;

import java.util.List;
import java.util.logging.Logger;

import jp.gr.java_conf.ryz.summarize.client.SummarizeService;
import jp.gr.java_conf.ryz.summarize.client.SummarizeServiceAsync;
import jp.gr.java_conf.ryz.summarize.client.view.SummarizeView;
import jp.gr.java_conf.ryz.summarize.shared.model.Board;
import jp.gr.java_conf.ryz.summarize.shared.model.Category;
import jp.gr.java_conf.ryz.summarize.shared.model.ThreadDetail;
import jp.gr.java_conf.ryz.summarize.shared.model.Thred;


import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SummarizePresenterImpl implements SummarizePresenter
{
	
	/**
	 * logger.
	 */
	private static final Logger _logger = Logger.getLogger(SummarizePresenterImpl.class.getName());
	
	/**
	 * SummarizeService remote proxy.
	 */
	private final SummarizeServiceAsync _summarizeService = GWT.create(SummarizeService.class);
	
	private SummarizeView _view;
	
	public SummarizePresenterImpl(SummarizeView view)
	{
		_view = view;
	}
	
	@Override
	public void initCategories()
	{
		_summarizeService.getCategories(new AsyncCallback<List<Category>>()
		{

			@Override
			public void onFailure(Throwable caught)
			{
				GWT.log("called onFailure.");
				
				postProcedure();
			}

			@Override
			public void onSuccess(List<Category> result)
			{
				_logger.info("called onSuccesse.");
				
				_view.setCategories(result);
				
				postProcedure();
			}
			
			private void postProcedure()
			{
			}
		});
	}

	@Override
	public void updateThreads()
	{
		_view.showLoadingPanel();
		
		Board board = _view.getSelectedBoard();
		_summarizeService.getThreads(board.getUrl(), new AsyncCallback<List<Thred>>()
		{

			@Override
			public void onFailure(Throwable caught)
			{
				postProcedure();
			}

			@Override
			public void onSuccess(List<Thred> result)
			{
				_view.setThreads(result);
				
				postProcedure();
			}
			
			
			private void postProcedure()
			{
				_view.hideLoadingPanel();
			}
		});
	}

	@Override
	public void updatePosts()
	{
		_view.showLoadingPanel();
		
		final Thred thread = _view.getSelectedThread();	
		
		_summarizeService.getThreadDetail(thread.getDatUrl(), new AsyncCallback<ThreadDetail>()
		{

			@Override
			public void onFailure(Throwable caught)
			{
				postProcedure();
			}

			@Override
			public void onSuccess(ThreadDetail result)
			{
				_view.setThreadDetail(thread, result);
				
				postProcedure();
			}
			
			
			private void postProcedure()
			{
				_view.hideLoadingPanel();
			}
		});
	}

	@Override
	public void openExportDialog()
	{
		_view.openExportDialog();
	}

	@Override
	public void selectAllPost() {
		_view.selectAllPost();
	}

	@Override
	public void clearSelectAllPost() {
		_view.clearSelectAllPost();
	}

	@Override
	public void enableAllBold() {
		_view.enableAllBold();
	}

	@Override
	public void disableAllBold() {
		_view.disableAllBold();
	}
}
